package typejump.objects;

import typejump.engine.Animation;
import typejump.engine.CollisionProps;
import typejump.engine.GameAction;
import typejump.sound.SoundManager;

/**
 * Items that can be picked up by the Player (powerups, etc)
 * @author g
 *
 */
public class Item extends Sprite {

	private GameAction action;
	private String consumeSound = null;

	/**
	 * Creates a new Item
	 * @param anim - the default animation to loop
	 * @param sound - the sound file to play when it's picked up (eg, "jump.wav")
	 * @param action - the GameAction to activate when the item is picked up
	 */
	public Item(Animation anim, String sound, GameAction action) {
		setDefaultAnimation(anim);
		this.consumeSound = sound;
		this.action = action;
		canFall = true;
		physical = true;
		dynamic = true;
		moveable = true;
		setCollision(CollisionProps.T_ITEM, CollisionProps.T_GEOM);
		scaleToImage();
	}
	/**
	 * Creates a new Item
	 * @param anim - the default animation to loop
	 * @param action - the GameAction to activate when the item is picked up
	 */
	public Item(Animation anim, GameAction action) {
		this(anim, null, action);
	}
	/**
	 * Creates a new Item
	 * @param anim - the default animation to loop
	 */
	public Item(Animation anim) {
		this(anim, null, null);
	}
	
	/** Creates a new item with no animation or sound or anything */
	public Item() {
		this(null, null, null);
	}
	
	/** Sets the filename of the sound that will be played when
	 * this item is picked up.
	 * @param sound
	 */
	public void setConsumeSound(String sound) {
		this.consumeSound = sound;
	}
	
	/**
	 * Sets the GameAction that will be called when
	 * this item is picked up.
	 * @param act
	 */
	public void setAction(GameAction act) {
		this.action = act;
	}
	
	public void collide(GameObject other) {
		if (shouldRemove()) return;
		if (other instanceof Actor) {
			if (action != null)
				action.act(this, other);
			if (consumeSound != null)
				SoundManager.PLAYER.play(consumeSound);
			remove();
		}
	}
}
